#ifdef VAMPS
% A simple S-Lang script to generate a report from a vamps
% output file. Use vamps -I rep.sl to run this. 
% The rinmen function speeds up reading of the file but
% uses a _LOT_ of memory with large output file. Usually 
% adequate performance can be obtained by using an index file.
% See report.sl


define mcat(inf, fp)
{
	variable fpi, fpo, ol;

	fpi = fopen(inf,"r");

	if (fpi != -1)
		if (fp != -1){
			while(fgets(fpi) > 0) {
				ol = ();
				() = fputs(ol,fp);
			}
		}
		() = fclose(fpi);
}
define rep(infile,outfile)
{
   variable fp;
   variable s;
   variable z,k,h,theta,ksat,i,pt;

	
   fp = fopen(outfile,"w");
   if (fp == -1)
     fp = stdout;
   
   s = Sprintf("Report of the file %s generated by rep.sl\n",infile,1);
   () = fputs(s,fp);
   () = fputs ("GENERAL------------------------------------------------------------------\n",fp);
   () = fputs ("The command that generated this file was: \n",fp);
   () = fputs (getdefstr("header","command","NOT FOUND",infile,0),fp);
   () = fputs ("\nRun started at: ",fp);
   () = fputs (getdefstr("header","run_start_time","NOT FOUND",infile,0),fp);
   () = fputs ("\nRun ended at: ",fp);
   () = fputs (getdefstr("trailer","run_end_time","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal number of itterations: ",fp);
   () = fputs (getdefstr("final","nr_it","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal number of cals to timestep: ",fp);
   () = fputs (getdefstr("final","timestep_calls","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal number of call to tridiag: ",fp);
   () = fputs (getdefstr("final","nr_tri","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal number of call to banddiag: ",fp);
   () = fputs (getdefstr("final","nr_band","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal number of call to sat: ",fp);
   () = fputs (getdefstr("final","nr_sat","NOT FOUND",infile,0),fp);
   () = fputs ("\n\nINITIAL------------------------------------------------------------------\n",fp);
   
   () = fputs ("Initial water volume of profile: ",fp);
   () = fputs (getdefstr("initial","volini","NOT FOUND",infile,0),fp);
   
   () = fputs ("\nSaturated water volume of profile: ",fp);
   () = fputs (getdefstr("initial","volsat","NOT FOUND",infile,0),fp);
   
   () = fputs ("\n\nTIME---------------------------------------------------------------------",fp);
   () = fputs ("\nNumber of timesteps in run: ",fp);
   () = fputs (getdefstr("initial","steps","NOT FOUND",infile,0),fp);
   
   () = fputs ("\n\nSOIL---------------------------------------------------------------------",fp);
   () = fputs ("\nTotal soil layers of profile: ",fp);
   () = fputs (getdefstr("initial","layers","NOT FOUND",infile,0),fp);

   () = fputs ("\n\nINITIAL PROFILE----------------------------------------------------------",fp);
   (z, pt) = getdefar("initial","z",infile,1); 
   (h, pt) = getdefar("initial","h",infile,1); 
   (k, pt) = getdefar("initial","k",infile,1); 
   (theta, pt) = getdefar("initial","theta",infile,1); 
   (ksat, pt) = getdefar("initial","ksat",infile,1); 
   fprintf(fp,"\nz	theta		h		k 	ksat\n",0);
   for (i = 0; i < pt; i++){
   	fprintf(fp,"%f\t%f\t%f\t%f\t%f\n",z[i],theta[i],h[i],k[i],ksat[i],5);
   }
   
   () = fputs ("\n\nWATER BAL. SUM.---------------------------------------------------------",fp);
   () = fputs ("\nTotal precipitation: ",fp);
   () = fputs (getdefstr("final","precipitation","NOT FOUND",infile,0),fp);
   
   () = fputs ("\nTotal transpiration: ",fp);
   () = fputs (getdefstr("final","transpiration","NOT FOUND",infile,0),fp);
   
   () = fputs ("\nTotal interception: ",fp);
   () = fputs (getdefstr("final","interception","NOT FOUND",infile,0),fp);
   
   () = fputs ("\nTotal soilevaporation: ",fp);
   () = fputs (getdefstr("final","soilevaporation","NOT FOUND",infile,0),fp);

   () = fputs ("\nTotal rootextraction: ",fp);
   () = fputs (getdefstr("final","rootextract","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal drainage: ",fp);
   () = fputs (getdefstr("final","drainage","NOT FOUND",infile,0),fp);
   () = fputs ("\nTotal surface_runoff: ",fp);
   () = fputs (getdefstr("final","surface_runoff","NOT FOUND",infile,0),fp);
   () = fputs ("\nInitial storage: ",fp);
   () = fputs (getdefstr("final","initial_storage","NOT FOUND",infile,0),fp);
   () = fputs ("\nFinal storage: ",fp);
   () = fputs (getdefstr("final","final_storage","NOT FOUND",infile,0),fp);
   () = fputs ("\nChange in storage: ",fp);
   () = fputs (getdefstr("final","delta_storage","NOT FOUND",infile,0),fp);
   () = fputs ("\nPercent mass-balance err: ",fp);
   () = fputs (getdefstr("final","mass_balance","NOT FOUND",infile,0),fp);
   () = fputs ("\n\n",fp);

   fprintf(fp,"INPUT FILE ----------------------------------------------------------------\n",0);
    variable mlst = getdefstr("header","infilename","NOT FOUND",infile,0);
   () = rinmem(mlst);
   if (fp == stdout)
   	writememini("-",mlst);
   else	{
   	writememini("tt.tt",mlst);
	() = fflush(fp);
	mcat("tt.tt",fp);
   }
   if (fp != stdout)
     () = fclose(fp);
}
#else
fputs("This script needs to be run with Vamps\n",stderr);
#endif
